package com.fly.estore.dao.Impl;

import com.fly.estore.dao.BaseDAO;
import com.fly.estore.dao.MemberPointDAO;
import com.fly.estore.domain.MemberPoint;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/5/10.
 */
@Repository("MemberPointDAO")
public class MemberPointDAOImpl extends BaseDAO<MemberPoint> implements MemberPointDAO {

    public int addMemberPoint(final MemberPoint memberPoint) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("insert into member_point(member_point_id, member_id, point, description, create_id, create_time)")
                .append("values(?, ?, ?, ?, ?, ?)");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, memberPoint.getMemberPointId());
                ps.setString(i++, memberPoint.getMemberId());
                ps.setInt(i++, memberPoint.getPoint());
                ps.setString(i++, memberPoint.getDescription());
                ps.setString(i++, memberPoint.getCreateId());
                ps.setString(i++, memberPoint.getCreateTime());
            }
        });
        return update;
    }

    public int updateMemberPoint(final MemberPoint memberPoint) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("update member_point set")
                .append(" member_id = ?,")
                .append(" point = ?,")
                .append(" description = ?,")
                .append(" last_update_id = ?,")
                .append(" last_update_time = ?")
                .append(" where member_point_id = ?");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, memberPoint.getMemberId());
                ps.setInt(i++, memberPoint.getPoint());
                ps.setString(i++, memberPoint.getDescription());
                ps.setString(i++, memberPoint.getLastUpdateId());
                ps.setString(i++, memberPoint.getLastUpdateTime());
                ps.setString(i++, memberPoint.getMemberPointId());
            }
        });
        return update;
    }

    public int deleteById(final String memberPointId) {
        int update = 0;
        String sql = "delete from member_point where member_point_id = ?";
        update = getJdbcTemplate().update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, memberPointId);
            }
        });
        return update;
    }

    public int deleteByMemberId(final String memberId) {
        int update = 0;
        String sql = "delete from member_point where member_id = ?";
        update = getJdbcTemplate().update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, memberId);
            }
        });
        return update;
    }

    public List<MemberPoint> getListByMemberId(String memberId) {
        List<MemberPoint> pointList = new ArrayList<MemberPoint>();
        String sql = "select * from member_point where member_id = ? order by create_time desc";
        pointList = getJdbcTemplate().query(sql, new PointRowMapper(), new Object[]{memberId});
        return pointList;
    }

    public int getMemberPoint(String memberId) {
        int totalPoint = 0;
        String sql = "select ifnull(sum(point), 0) from member_point where member_id = ?";
        totalPoint = getJdbcTemplate().queryForObject(sql, new Object[]{memberId}, Integer.class);
        return totalPoint;
    }

    public class PointRowMapper implements RowMapper<MemberPoint> {
        public MemberPoint mapRow(ResultSet rs, int rowNum) throws SQLException {
            MemberPoint point = new MemberPoint();
            point.setMemberPointId(rs.getString("member_point_id"));
            point.setMemberId(rs.getString("member_id"));
            point.setPoint(rs.getInt("point"));
            point.setDescription(rs.getString("description"));
            point.setCreateId(rs.getString("create_id"));
            point.setCreateTime(rs.getString("create_time"));
            point.setLastUpdateId(rs.getString("last_update_id"));
            point.setLastUpdateTime(rs.getString("last_update_time"));
            return point;
        }
    }
}
